// 导入数据库
const db = require('../db')

// 获取学生信息的处理函数
exports.getAllStuInfo = (req, res) => {
    const sql = `
  select a.id, a.stu_id, a.stu_name, b.class_name, a.sex, a.age, a.tel, a.is_delete
  from stu_stuinfo a
  inner join stu_class b
  on a.class_id=b.id
  where a.is_delete=0
  limit ?, ?
  `

    const count = +req.query.count
    const startIndex = (req.query.pageNum - 1) * count
    db.query(sql, [startIndex, count], (err, results1) => {
        if (err) return res.sd(err.message)

        if (results1.length < 1) return res.sd('获取学生信息失败')

        const sql = `select count(id) as total from stu_stuinfo where is_delete=0`

        db.query(sql, (err, results2) => {
            if (err) return res.sd(err.message)

            if (results2.length < 1) return res.sd('获取信息信息失败')

            res.send({
                status: 0,
                message: '获取学生信息成功',
                data: results1,
                total: results2[0].total
            })
        })
    })
}

//添加学生信息处理函数
exports.addStuInfo = (req, res) => {
    const sql = `select * from stu_stuinfo where stu_id=?`

    db.query(sql, [req.body.stu_id, req.body.stu_name], (err, results) => {
        if (err) return res.sd(err.message)

        if (results.length === 1) return res.sd('学生信息已存在')

        const sql = `insert into stu_stuinfo set ?`

        db.query(sql, req.body, (err, results) => {
            console.log(err);
            console.log(results);
            if (err) return res.sd(err.message)

            if (results.affectedRows !== 1) return res.sd('添加学生信息失败')

            res.send({
                status: 0,
                message: '添加学生信息成功'
            })
        })
    })


}

// 更新学生信息的处理函数
exports.updateStuInfo = (req, res) => {
    const sql = `select * from stu_stuinfo where id=?`

    db.query(sql, req.body.id, (err, results) => {
        if (err) return res.sd(err.message)

        if (results.length !== 1) return res.sd('学生信息修改失败')

        const sql = `update stu_stuinfo set ? where id=?`

        db.query(sql, [req.body, req.body.id], (err, results) => {
            if (err) return res.sd(err.message)

            if (results.affectedRows !== 1) return res.sd('学生信息修改失败')

            res.send({
                status: 0,
                message: '修改学生信息成功'
            })
        })
    })
}

// 删除学生信息的处理函数
exports.deleteStuInfo = (req, res) => {
    const sql = `select * from stu_stuinfo where id=?`

    db.query(sql, req.body.id, (err, results) => {
        if (err) return res.sd(err.message)

        if (results.length !== 1) return res.sd('学生信息删除失败')

        const sql = `update stu_stuinfo set is_delete=1 where id=?`

        db.query(sql, req.body.id, (err, results) => {
            if (err) return res.sd(err.message)

            if (results.affectedRows !== 1) return res.sd('学生信息删除失败')

            res.send({
                status: 0,
                message: '修改学生删除成功'
            })
        })
    })
}

// 根据id获取学生信息的处理函数
exports.getStuInfoById = (req, res) => {
    const sql = `select * from stu_stuinfo where id=?`

    db.query(sql, req.query.id, (err, results) => {
        if (err) return res.sd(err.message)

        if (results.length !== 1) return res.sd('学生信息获取失败')

        const sql = `
        select a.id, a.stu_id, a.stu_name, a.class_id, b.class_name, a.sex, a.tel, a.is_delete
        from stu_stuinfo a
        inner join stu_class b
        on a.class_id=b.id
        where a.is_delete=0 and a.id=?
        `

        db.query(sql, req.query.id, (err, results) => {
            if (err) return res.sd(err.message)

            if (results.length < 1) return res.sd('学生信息获取失败')

            res.send({
                status: 0,
                message: '学生信息获取成功',
                data: results,
            })
        })
    })
}